import pymysql
from pymysql.cursors import DictCursor
import json,os
from tools import  config

def getdata(sql,is_dic = True,param=[]):
   conn = pymysql.connect(**config.mysqldbinfo)
   if is_dic:
      cursor = conn.cursor(DictCursor)
   else:
      cursor = conn.cursor()
   cursor.execute(sql, param)
   alldata = cursor.fetchall()
   return alldata

def getdata_param(sql,param=[]):
   conn = pymysql.connect(**config.mysqldbinfo)
   cursor = conn.cursor(DictCursor)
   cursor.execute(sql, param)
   alldata = cursor.fetchall()
   return alldata

def execsql(sql,param=[]):
   connection = pymysql.connect(**config.mysqldbinfo)
   try:
       with connection.cursor() as cursor:
           # Create a new record
           cursor.execute(sql,param)
       # connection is not autocommit by default. So you must commit to save
       # your changes.
       connection.commit()
   finally:
       connection.close()